<template>
  <div class="page">
    <div class="page-list">
      <div
        class="teacherClass"
        v-if="
          teacherClass.school_name ||
          teacherClass.gradeName ||
          teacherClass.class_name
        "
      >
        <p class="teacherP">
          <span>
            {{ teacherClass.school_name ? teacherClass.school_name : '' }}
          </span>
        </p>
        <p class="teacherP">
          <span>
            {{ teacherClass.gradeName ? teacherClass.gradeName : '' }}
          </span>
          <span>
            {{ teacherClass.class_name ? teacherClass.class_name : '' }}
          </span>
        </p>
      </div>
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-empty v-show="dataList.length <= 0" description="" />
        <van-list
          v-model="loading"
          :finished="finished"
          :immediate-check="false"
          :error.sync="errorLoading"
          error-text="请求失败，点击重新加载"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <div
            class="page-list-item"
            v-for="(item, index) in dataList"
            :key="index"
          >
            <div
              class="page-list-item-inner"
              :class="{ 'page-list-item-active': item.isChecked }"
            >
              <div class="w100 list-card">
                <van-row type="flex" justify="start" class="w100">
                  <van-col span="24" class="mid bold">
                    {{ item.name }}
                  </van-col>
                </van-row>
                <div class="desc w100">{{ item.tel }}</div>
                <div class="page-list-item-inner-bottom w100">
                  <van-button
                    class="page-list-item-inner-bottom-btn"
                    size="small"
                    :color="item.isChecked ? '#A3A3A3' : '#D58913'"
                    type="info"
                    native-type="button"
                    :text="item.isChecked ? '取消选中' : '选中'"
                    @click="clickAction('check', item)"
                  />
                  <van-button
                    class="page-list-item-inner-bottom-btn"
                    size="small"
                    color="#D58913"
                    type="info"
                    native-type="button"
                    text="审批"
                    @click="clickAction('examine', item)"
                  />
                </div>
              </div>
            </div>
          </div>
        </van-list>
      </van-pull-refresh>
    </div>
    <div class="bottom-bar">
      <van-button
        class="bottom-bar-btn"
        block
        size="large"
        color="#D58913"
        type="info"
        text="批量审核"
        @click="clickAction('plExamine')"
      />
    </div>
    <van-dialog
      v-model="examineShow"
      :show-confirm-button="false"
      close-on-click-overlay
    >
      <div class="message">请核实是否进行操作</div>
      <div class="message-bottom">
        <van-button
          block
          size="large"
          text="审核不通过"
          @click="clickAction('noPass')"
        /><van-button
          block
          size="large"
          text="审核通过"
          @click="clickAction('pass')"
        />
      </div>
    </van-dialog>
  </div>
</template>

<script>
import {
  getExamineList,
  updateExamine,
  selectTeacherByCardNo,
} from '@/api/school'

export default {
  data() {
    return {
      data: {},
      dataList: [],
      route: {},
      page: {
        num: 1,
        total: 0,
      },
      loading: true,
      errorLoading: false,
      finished: false,
      refreshing: false,
      checkedList: [],
      examineShow: false,
      teacherClass: {},
    }
  },
  computed: {},
  watch: {},
  mounted() {
    this.route = this.$route.query
    this.init()
  },
  methods: {
    init() {
      this.loading = true
      this.onLoad()
    },
    getListAction() {
      const data = {
        pageNo: this.page.num,
        pageSize: 20,
        card_no: this.route.card_no,
        ...this.data,
      }
      selectTeacherByCardNo({ card_no: this.route.card_no }).then((result) => {
        if (result && result.data.errcode === 0) {
          this.teacherClass = result.data.data
        }
      })
      getExamineList(data).then((ret) => {
        const data = ret.data
        if (data.errcode === 0) {
          data.data.results.forEach((item) => {
            this.$set(item, 'isChecked', false)
          })
          this.dataList.push(...data.data.results)
          this.page.total = data.data.totalRecord
          this.page.num += 1
          if (data.data.pageNo >= data.data.totalPage) {
            this.finished = true
          }
        } else {
          this.errorLoading = true
        }
        this.loading = false
      })
    },
    onRefresh() {
      document.querySelector('html').scrollTop = '0'
      // 重置筛选数据
      this.checkedList = []
      // 清空列表数据
      this.finished = false
      this.refreshing = true
      // 重新加载数据
      this.loading = true
      this.page.num = 1
      this.onLoad()
    },
    onLoad() {
      // 异步更新数据
      // if (this.userInfo.cardNo) {
      if (this.refreshing) {
        this.dataList = []
        this.page.num = 1
        this.refreshing = false
      }
      this.getListAction()
      // } else {
      //   this.loading = false
      // }
    },

    async clickAction(type, item) {
      switch (type) {
        case 'check':
          item.isChecked = !item.isChecked
          if (item.isChecked) {
            this.checkedList.push({ id: item.id })
          } else {
            this.checkedList.forEach((checkedItem, checkedIndex) => {
              if (checkedItem.id == item.id) {
                this.checkedList.splice(checkedIndex, 1)
              }
            })
          }
          break
        case 'examine':
          this.$router.push({
            path: '/school/studentExamineDetail',
            query: {
              id: item.id,
            },
          })
          break
        case 'plExamine':
          if (this.checkedList.length == 0) {
            this.$toast('请至少选择一条信息')
            return
          }
          this.examineShow = true
          break
        case 'pass':
          this.updateExamine(0)
          break
        case 'noPass':
          this.updateExamine(3)
          break
      }
    },
    async updateExamine(del_flag) {
      this.$dialog
        .confirm({
          title: '是否确认操作',
          message: '',
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          confirmButtonColor: '#D58913',
          beforeClose: async (action, done) => {
            if (action === 'confirm') {
              const examineData = await updateExamine({
                ids: this.checkedList,
                del_flag: del_flag,
              })
              if (examineData.data.errcode == 0) {
                this.$toast('操作成功')
                this.examineShow = false
                setTimeout(() => {
                  this.onRefresh()
                }, 500)
                done()
              } else {
                this.$toast(examineData.data.errmsg)
                done()
              }
            } else {
              done()
            }
          },
        })
        .catch(() => {})
    },
  },
}
</script>

<style lang="less" scoped>
.page {
  width: 100%;
  height: 100vh;
  box-sizing: border-box;
  color: black;
  position: relative;
  .page-list {
    height: calc(100% - 50px);
    padding-top: 1.6em;
    .teacherClass {
      width: calc(100% - 20px);
      height: 60px;
      margin: 0 auto;
      background-color: #fff;
      border-radius: 10px;
      .teacherP {
        height: 30px;
        display: flex;
        justify-content: space-around;
        align-items: center;
        margin: 0;
      }
    }
    .van-pull-refresh {
      height: calc(100% - 60px);
      overflow: hidden auto;
    }
  }
  /deep/.van-search__content--round {
    border-radius: 10px;
  }
  /deep/.van-search__content {
    background-color: #fff;
  }

  &-search {
    margin: 0 10px;
    background-color: #fff;
    display: flex;
    position: sticky;
    top: 0;
    z-index: 10;
    border-radius: 10px;
    overflow: hidden;

    &-select {
      width: 40%;
    }
  }

  &-list {
    box-sizing: border-box;
    width: 100%;

    & > :not(:last-child) {
      border-bottom: 1px solid #ededed;
    }

    &-item {
      margin-bottom: 20px;
      position: relative;

      .arrow {
        position: absolute;
        right: 15px;
        top: 35%;
        width: 40px;
        height: 40px;
      }

      &-inner {
        margin: 15px 10px;
        padding: 20px 40px 20px 30px;
        min-height: 80px;
        text-align: left;
        border-radius: 20px;
        background: #fff;
        position: relative;
        overflow: hidden;

        &-bottom {
          display: flex;
          justify-content: space-around;
          margin-top: 20px;

          &-btn {
            width: 40%;
            border-radius: 5px;
          }
        }
      }

      &-active {
        box-shadow: 0px 0px 5px 3px rgba(255, 145, 0, 0.8);
      }
    }
  }
}
.list-card {
  display: flex;
  flex-wrap: wrap;
}
.desc {
  margin-top: 10px;
  font-weight: 400;
  font-size: 20px;
  text-align: left;
  color: #b4b4b4;
}
.bottom-bar {
  position: fixed;
  bottom: 0;
  width: 100%;
}
.message {
  padding: 20px;
  text-align: center;

  &-bottom {
    display: flex;
  }
}
</style>
